﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Domain = Sunboxsoft.ASMS.Server.Domain;

namespace Sunboxsoft.ASMS.Web.ApiControllers
{
    public class QueryController : ApiController
    {
        [HttpGet]
        public PagedResult<Domain.View.VBusiness> BusinessPagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryBusinessPagedList(filter, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VStation> PrivateStationPagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryStationPagedList(filter, WebUtility.CookieUtility.GetBusinessId(), status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VStation> StationPagedList(string filter, Guid? businessId, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryStationPagedList(filter, businessId, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EStaff> PrivateStaffPagedList(string filter, Guid? stationId, int status, int start, int limit)
        {
            Guid? businessId = WebUtility.CookieUtility.GetBusinessId();
            if (stationId == null)
            {
                return new Domain.Service.QueryService().QueryStaffPagedList(filter, businessId.Value,
                    status, start, limit);
            }
            return new Domain.Service.QueryService().QueryStaffPagedList(filter, businessId.Value, stationId.Value,
                status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EStaff> StaffPagedList(string filter, Guid businessId, Guid? stationId, int status, int start, int limit)
        {
            if (stationId == null)
            {
                return new Domain.Service.QueryService().QueryStaffPagedList(filter, businessId, status, start, limit);
            }
            return new Domain.Service.QueryService().QueryStaffPagedList(filter, businessId, stationId.Value, status, start, limit);
        }
       
        [HttpGet]
        public PagedResult<Domain.Entity.EStaff> AdminStaffPagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryAdminStaffPagedList(filter, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EMasterDataItem> MasterDataItemPagedList(string masterDataCode, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryMasterDataItemPagedList(masterDataCode, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EServiceCategory> ServiceCategoryPagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryServiceCategoryPagedList(null, null, status, start, limit);
        }

        [HttpGet]
        public ListResult<Domain.Entity.EServiceCategory> ServiceCategoryList()
        {
            PagedResult < Domain.Entity.EServiceCategory > result = new Domain.Service.QueryService().QueryServiceCategoryPagedList(null, null, Domain.Constant.CommonStatus.Enabled, 0, Domain.Constant.PageLimitAll);

            if (result.IsSuccess)
            {
                return new ListResult<Domain.Entity.EServiceCategory>(result.Data.Items);
            }
            return new ListResult<Domain.Entity.EServiceCategory>(result.ErrorCode, result.Message);
        }

        [HttpGet]
        public PagedResult<Domain.View.VServiceItem> ServiceItemPagedList(string filter, Guid? categoryId, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryServiceItemPagedList(filter, categoryId, businessId, stationId, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VServiceItem> PrivateServiceItemPagedList(string filter, Guid? categoryId, Guid? stationId, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryServiceItemPagedList(filter, categoryId, WebUtility.CookieUtility.GetBusinessId(), stationId, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VSupplier> SupplierPagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QuerySupplierPagedList(filter, null, null, status, start, limit);
        }
        [HttpGet]
        public PagedResult<Domain.View.VSupplier> PrivateSupplierPagedList(string filter, int status, int start, int limit)
        {
            if(status == Domain.Constant.CommonStatus.WaitAuditing
                || status == Domain.Constant.CommonStatus.Deleted)
            {
                return new Domain.Service.QueryService().QuerySupplierPagedList(filter, WebUtility.CookieUtility.GetBusinessId(), null, status, start, limit);
            }
            return new Domain.Service.QueryService().QuerySupplierPagedList(filter, null, null, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VWarehouse> PrivateWarehousePagedList(string filter, Guid? stationId, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryWarehousePagedList(filter, WebUtility.CookieUtility.GetBusinessId(), stationId, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VWarehouse> WarehousePagedList(string filter, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryWarehousePagedList(filter, businessId, stationId, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VMaterial> PrivateMaterialPricePagedList(string filter, Guid? stationId, string category, string brand, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryMaterialPagedList(filter, WebUtility.CookieUtility.GetBusinessId(), stationId, category, brand, Domain.Constant.CommonStatus.Enabled, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VMaterial> MaterialPricePagedList(string filter, Guid? businessId, Guid? stationId, string category, string brand, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryMaterialPagedList(filter, businessId, stationId, category, brand, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.View.VMaterial> MaterialPagedList(string filter, string category, string brand, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryMaterialPagedList(filter, null, null, category, brand, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EPayItem> PayItemPagedList(int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryPayItemPagedList(null, null, status, start, limit);
        }

        [HttpGet]
        public PagedResult<Domain.Entity.EPackage> PackagePagedList(string filter, int status, int start, int limit)
        {
            return new Domain.Service.QueryService().QueryPackagePagedList(filter, null, null, status, start, limit);
        }
    }
}
